package com.etoak.listener;

import com.alibaba.fastjson.JSONObject;
import com.etoak.bean.Email;
import com.etoak.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

/**
 * @program: activemq-consumer-email
 * @description: 消费者 监听
 * @author: 管浩杰
 * @create: 2021-03-17 15:03
 **/


@Service
public class EmailMsgListener implements MessageListener {

    @Autowired
    private EmailService emailService;

    @Override
    public void onMessage(Message message) {
        TextMessage text = (TextMessage)message;

        // 接收 并签收消息
        try{
            String json = text.getText();
            System.out.println("接受到的消息" + json);
            Email email = (Email)JSONObject.parse(json);
            emailService.send(email);
            text.acknowledge();
        }catch(JMSException e){
            e.printStackTrace();
        }
    }
}
